(:
  A procedure to select connect / disconnect event
:)
declare boundary-space strip;
import module namespace
    jj = "http://kumquat.emse.fr/utilitaires"
    at "jjutils.xq" ;

(: Modifiable color for texts : set a color or "" for no color :)
declare variable $evcolor := "darkgreen" ;
declare variable $evbgcolor := "" ;

(:  Internal declarations :)
declare variable $colorattr :=
  (  if ($evcolor != "") then attribute color { $evcolor } else () ,
     if ($evbgcolor != "") then attribute bgcolor { $evbgcolor } else ()  ) ;
(:  Return result  :)
<display>{
let $t := $arguments[1]
let $d := doc($t)/trace/event
for $e at $p in $d
where $e[server/connection or server/syn or control/info]
return
  <item>{
    <info name="type">event</info> ,
    <info name="time">{ $e/time }</info>,
    if ($e/@room) then
       <info name="room">{string($e/@room)}</info>
    else
       (),
    <info name="src-anchor">{
      <anchor>{
        <doc>{ $t }</doc>,
        <path>{jj:build-Path($e)}</path>
      }</anchor>
    }</info>,
    if ($e/server/connection) then
        if ($e//identity/@user) then
            <info name="user">{string($e//identity/@user)}</info>
        else
        if ($e/@user) then
            <info name="user">{string($e/@user)}</info>
        else
            (),
        if ($e/server/connection[@state="new"]) then
            <info name="content">
                <text>{ $colorattr,
                    string($e//identity/@user), "s'est connecté."
               }</text>
           </info>
        else
        if ($e/server/connection[@state="end"]) then
            <info name="content">
                <text>{ $colorattr,
                    string($e/@user), "s'est déconnecté."
                }</text>
           </info>
        else
           ()
    else
    if ($e/server/syn) then
         ( <info name="content">
                <text>{ $colorattr, "synchronisation du serveur"} </text>
           </info>,
           <info name="user">{string($e/@user)}</info> )
    else
    if ($e/control/info) then
        (<info name="user">{fn:tokenize(fn:data($e/control/info), "\s+")[1]}</info>,
        <info name="content">
        <text>{ $colorattr, 
             string($e/control/info)
          }</text>
        </info>)
    else
        (<info name="content">
        <text>{ $colorattr,
            "unknown event"
        }</text>
        </info>)
  }</item>
}</display>
